package com.gg.widget

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.util.Log


/**
 * Creator : GG
 * Date    : 2018/4/13
 * Mail    : gg.jin.yu@gmai.com
 * Explain :
 */
object LoadUtils {

    @SuppressLint("StaticFieldLeak")
    private var loadingDialog: LoadingDialog? = null

    fun show(context: Context) {
        try {
            if (loadingDialog != null) {
                loadingDialog = null
            }

            if ((context as Activity).isDestroyed)
                return

            loadingDialog = LoadingDialog(context)
            loadingDialog?.setCancelable(true)
            loadingDialog?.setCanceledOnTouchOutside(false)
            loadingDialog?.setOnCancelListener({ hidden() })
            loadingDialog?.show()


        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    fun show(context: Context, msg: String) {
        try {
            if (loadingDialog != null) {
                loadingDialog = null
            }

            if ((context as Activity).isDestroyed)
                return
            
            loadingDialog = LoadingDialog(context)
            loadingDialog?.setText(msg)
            loadingDialog?.setCanceledOnTouchOutside(false)

            loadingDialog?.setOnCancelListener({ hidden() })

            loadingDialog?.show()
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    fun hidden() {
        try {

            Log.d("LoadUtils", "销毁")
            if (loadingDialog != null) {
                loadingDialog?.dismiss()
                loadingDialog?.setTextViewNull()
                loadingDialog = null
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }


}